﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TDgame_TankHP : MonoBehaviour
{

    public int HP;
    public int maxHP;
    public GameObject fireEffectObj;//火焰特效
    public GameObject replaceDeadObj;//残骸物体
    private Slider hp_silder;


    private void Awake()
    {
        HP = maxHP;
        hp_silder = transform.Find("HPBar/Slider").GetComponent<Slider>();
        hp_silder.maxValue = maxHP;
        updateHP();
        if (fireEffectObj != null)
        {
            fireEffectObj.SetActive(false);
        }
    }

    public void updateHP()
    {
        hp_silder.value = HP;
    }

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {

        if (HP < maxHP / 2)
            fireEffectObj.SetActive(true);
        if (HP <= 0)
        {
            //坦克爆炸销毁
            if (replaceDeadObj != null)
                Instantiate(replaceDeadObj, transform.position, transform.rotation);
            //实例化爆炸粒子
            //销毁自己
            Destroy(gameObject);
        }
    }

    public void TakeDamage(int damage)
    {
        if (HP > damage)
        {
            HP -= damage;
            hp_silder.value = HP;//更新血条
        }
        else
        {
            HP = 0;
            hp_silder.value = HP;//更新血条
        }
    }
}
